package NewTmas.Listeners;

import taskmaster.auth.User;
import taskmaster.job.TaskCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/19/11
 * Time: 10:31 AM
 * To change this template use File | Settings | File Templates.
 */
public class HelpFileListener implements ActionListener{

    String job;
    String task;
    Date date;
    TaskCollection taskCollection;
    JDesktopPane dtp;
    JTextArea textArea;
    JInternalFrame helpfileFrame;
    File f;
    JButton saveBtn;
    User user;


    public HelpFileListener(String job, String task, Date date, TaskCollection taskCollection, JDesktopPane dtp, User user) {
        this.job = job;
        this.task = task;
        this.date = date;
        this.taskCollection = taskCollection;
        this.dtp = dtp;
        this.helpfileFrame =new JInternalFrame("Help: "+job +":"+task);
        textArea=new JTextArea();
        this.user=user;


    }

    public HelpFileListener(File f, String job, String task, Date date, TaskCollection taskCollection, JDesktopPane dtp, User user) {
        this.job = job;
        this.task = task;
        this.date = date;
        this.taskCollection = taskCollection;
        this.dtp = dtp;
        this.helpfileFrame =new JInternalFrame("Help: "+job +":"+task);
        textArea=new JTextArea();
        this.user=user;
        this.f=f;


    }

    public void actionPerformed(ActionEvent e) {

        if(f!=null) {

            showFile(f);
            createHelpFrame();
        }

        else
        {     String TMS_HELPDIR = System.getProperty("TMS_HELPDIR");
            if (TMS_HELPDIR == null)
            {
                throw new NullPointerException("taskmaster.controller.ViewTasks:findHelpFilePath(): TMS_HELPDIR env var not set in System properties");
            }
            StringBuffer findFile = new StringBuffer(TMS_HELPDIR);
            findFile.append(File.separator);
            findFile.append(job);
            this.f = new File(findFile.toString(), task + ".txt");
            try {
                f.createNewFile() ;

            } catch (IOException e1) {
                JOptionPane.showMessageDialog(null, e1.getMessage(), "New help file could not be created: ", JOptionPane.ERROR_MESSAGE);
            }
            showFile(f);
            createHelpFrame();
        }
    }


    public void showFile(File f) {
        FileInputStream fis=null ;
        try {
            if(f!=null)
                fis = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String strLine ;
            while ((strLine = br.readLine()) != null)   {
                textArea.append(strLine);
                textArea.append("\n");
            }
            dtp.add(helpfileFrame);
            helpfileFrame.toFront();
            helpfileFrame .setVisible(true);
        } catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"No help file found or created",JOptionPane.ERROR_MESSAGE);
            // ex.printStackTrace();
        }

    }


    public void createHelpFrame(){
        textArea.setFont(new Font("DialogInput", Font.PLAIN, 12));
        textArea.setColumns(6);
//        textArea.setLineWrap(true);
        helpfileFrame.setSize(500,525);
        helpfileFrame.setIconifiable(true);
        helpfileFrame.setMaximizable(true);
        helpfileFrame.setResizable(true);
        helpfileFrame.setClosable(true);
        saveBtn = new JButton("Save");
        helpfileFrame.setVisible(true);
        helpfileFrame.setSize(500, 525);
        JScrollPane scrollPane = new JScrollPane(textArea);
        GroupLayout groupLayout = new GroupLayout(helpfileFrame.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(GroupLayout.Alignment.TRAILING, groupLayout.createSequentialGroup()
                                                .addComponent(saveBtn)
                                                .addGap(20))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                                                .addContainerGap())))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(23)
                                .addComponent(saveBtn)
                                .addGap(18)
                                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                                .addContainerGap())
        );
        helpfileFrame.getContentPane().setLayout(groupLayout);

        helpfileFrame.getContentPane().add(saveBtn);



        helpfileFrame.getContentPane().add(scrollPane);

        saveBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String toWrite=textArea.getText();


                if(toWrite!=null) {

                    BufferedWriter out = null;
                    try {
                        out = new BufferedWriter(new FileWriter(f));
                        out.write(toWrite);
                        out.write("\n --------------------------------");
                        out.write("\n edited by "+user.getUsername()+ " "+Calendar.getInstance().getTime());
                        out.close();
                    } catch (IOException e1) {
                        JOptionPane.showMessageDialog(null,"Error retrieving/saving the Help File"+e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                        e1.printStackTrace();
                    }


                }

            }
        } );





    }




}
